﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SPIWareMVVM.ViewModels;
using DataAccess;
using System.Collections.ObjectModel;

namespace SPIWareMVVM.Views
{
    /// <summary>
    /// Interaction logic for MaterialList.xaml
    /// </summary>
    public partial class MaterialList : UserControl
    {
        Window1 mainWindow { get; set; }
        ApplicationViewModel applicationViewModel { get; set; }

        public MaterialList()
        {
            InitializeComponent();
            ObservableCollection<MachinePress> list = ApplicationData.ocMachinePresses;
            editMaterial.cboMachinePress.ItemsSource = list;
            ObservableCollection<MaterialType> mtList = ApplicationData.ocMaterialTypes;
            editMaterial.cboMaterialTypes.ItemsSource = mtList;
        }
        
        protected void Materials_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            editMaterial.DataContext = null;

            mainWindow = (Window1)Window.GetWindow((ListBox)sender);
            if (mainWindow != null) applicationViewModel = (ApplicationViewModel)((ObjectDataProvider)mainWindow.Resources["applicationVM"]).ObjectInstance;
            if (mainWindow.workOrdersList.Visibility == System.Windows.Visibility.Visible) mainWindow.workOrdersList.Visibility = System.Windows.Visibility.Collapsed;

            if (e.RemovedItems.Count > 0)
            {
                editMaterial.Visibility = Visibility.Collapsed;
                dpMaterialsEdit.Visibility = Visibility.Collapsed;
            }

            if (e.AddedItems.Count > 0)
            {
                if (e.AddedItems[0].GetType().Name == "CustomerPart")
                    applicationViewModel.SelectedMaterial = ((CustomerPart)e.AddedItems[0]).Part;
                else applicationViewModel.SelectedMaterial = (Part)e.AddedItems[0];

                dpMaterialsEdit.Visibility = System.Windows.Visibility.Visible;

                editMaterial.DataContext = applicationViewModel.SelectedMaterial;
                editMaterial.Visibility = System.Windows.Visibility.Visible;
                //applicationViewModel.SelectedMaterial.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SelectedMaterial_PropertyChanged);
            }
        }

        //void SelectedMaterial_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        //{
        //    Part part = (Part)sender;
        //    switch (e.PropertyName)
        //    {
        //        case "AQ41Price": part.CalcTTLPackagingPiecePerPart(); break;
        //        case "AverageCycleTime_AS":
        //            part.CalcPartsPerHour_AS();
        //            part.CalcLaborSecondPerPart();
        //            break;
        //        case "TargetCycleTime": part.CalcPartsPerHour(); break;
        //        case "BoxPrice": part.CalcTTLPackagingPiecePerPart(); break;
        //        case "CostPerLB":
        //            part.CalcNaturalResin();
        //            part.CalcStartupScrapPrice();
        //            break;
        //        case "CostStartPress": part.CalcTTLCostPartAverage(); break;
        //        case "DyeCostPerLB":
        //            part.CalcDyeCostPerPart();
        //            part.CalcStartupScrapPrice();
        //            break;
        //        case "DyeCostPerPart":
        //            part.CalcResinDyePricePerPart();
        //            break;
        //        case "DyeMixRatio":
        //            part.CalcDyeCostPerPart();
        //            part.CalcStartupScrapPrice();
        //            break;
        //        case "LaborSecondPerPart": part.CalcLaborTTLPerPart(); break;
        //        case "LaborTTLPerPart":
        //            part.CalcTTLPrePartMaterialPackagingLabor();
        //            part.CalcAfterSetupRunTarget();
        //            part.CalcAfterSetupAvgCycleTime();
        //            break;
        //        case "MaterialCost": part.CalcTTLCostPrePartMaterialPackaging(); break;
        //        case "MoldSetupCost": part.CalcCostStartPress(); break;
        //        case "MoldSetupTime": part.CalcMoldSetupCost(); break;
        //        case "NaturalResin": part.CalcResinDyePricePerPart(); break;
        //        case "OpCost":
        //            part.CalcOpCostPerPart();
        //            part.CalcTTLPackagingPiecePerPart();
        //            part.CalcTTLPrePartMaterialPackagingLabor();
        //            part.CalcAfterSetupRunTarget();
        //            part.CalcAfterSetupAvgCycleTime();
        //            break;
        //        case "PartsPerBox":
        //            part.CalcBoxesPerPallet();
        //            part.CalcTTLPackagingPiecePerPart();
        //            break;
        //        case "PartsPerHour_AS":
        //            part.CalcOpCostPerPart();
        //            part.CalcTTLCostAvgCycleTime();
        //            part.CalcAfterSetupAvgCycleTime();
        //            part.CalcAfterMaterialsRunTarget();
        //            part.CalcAfterMaterialsAvgCycleTime();
        //            break;
        //        case "PartsPerHour":
        //            part.CalcTTLCostRunTarget();
        //            part.CalcAfterSetupRunTarget();
        //            break;
        //        case "PartsPerPallet": part.CalcBoxesPerPallet(); break;
        //        case "PartsPerShot":
        //            part.CalcPartsPerHour();
        //            part.CalcLaborSecondPerPart();
        //            part.CalcOverheadFactor();
        //            part.CalcPartsPerHour_AS();
        //            break;
        //        case "PartWeight":
        //            part.CalcNaturalResin();
        //            part.CalcDyeCostPerPart();
        //            break;
        //        case "Price":
        //            part.CalcTTLCostRunTarget();
        //            part.CalcTTLCostAvgCycleTime();
        //            part.CalcAfterSetupRunTarget();
        //            part.CalcAfterSetupAvgCycleTime();
        //            part.CalcAfterMaterialsRunTarget();
        //            part.CalcAfterMaterialsAvgCycleTime();
        //            break;
        //        case "PricePerBox": part.CalcTTLPackagingPiecePerPart(); break;
        //        case "ResinDyePricePerPart": part.CalcTTLCostPrePartMaterialPackaging(); break;
        //        case "StartupScrap": part.CalcStartupScrapPrice(); break;
        //        case "StartupScrapPrice": part.CalcCostStartPress(); break;
        //        case "TTLCostPartAverage":
        //            part.CalcTTLCostRunTarget();
        //            part.CalcTTLCostAvgCycleTime();
        //            part.CalcAfterSetupRunTarget();
        //            part.CalcAfterSetupAvgCycleTime();
        //            break;
        //        case "TTLCostPrePartMaterialPackaging":
        //            part.CalcTTLPrePartMaterialPackagingLabor();
        //            part.CalcAfterMaterialsRunTarget();
        //            part.CalcAfterMaterialsAvgCycleTime();
        //            break;
        //        case "TTLPackagingPiecePerPart": part.CalcTTLCostPrePartMaterialPackaging(); break;
        //        case "TTLPrePartMaterialPackagingLabor": part.CalcTTLCostPartAverage(); break;
        //    }
        //}
    }
}
